// pages/company/company.js
const app = getApp();
let that = null;
import store from "../../factory"
import * as Request from '../../request';
import * as mouble from '../../until';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModal: false,
    userType: "",
    editMod: false,
    fromIndex: 0,
    toIndex: 0,
    fromList: [],
    toList: [],
    statusIndex: 0,
    statusArray: [
      "否", "是"
    ],//默认显示位置
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    that.setData({
      userType: store.get("userType")
    })
    that.getRouteFrom();
    // that.getRouteTo();
  },

  add() {
    this.setData({
      editMod: true
    })
  },

  bindPicker(e) {
    this.setData({
      statusIndex: e.detail.value
    })
  },

  confirm() {
    this.setData({
      showModal: false
    })
  },

  fromListChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      fromIndex: e.detail.value
    })
    that.getRouteTo(e.detail.value)
  },

  toListChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      toIndex: e.detail.value
    })
  },

  getRouteFrom() {
    Request.getRouteFrom().then(res => {
      console.log(res);
      if (res.code == 200 && res.data) {
        that.setData({
          fromList: res.data
        })
        that.getRouteTo(0)
        console.log("formList", that.data.fromList)
      }
    })
  },

  getRouteTo(index) {
    Request.getRouteToFrom({
      fromCode: that.data.fromList[index].maerskCode
    }).then(res => {
      console.log(res);
      if (res.code == 200 && res.data) {
        that.setData({
          toList: res.data
        })
      }
    })
  },

  saveCompanyRoute() {
    let APPDATA = that.data;
    let postData = {
      fromCode: APPDATA.fromList[APPDATA.fromIndex].maerskCode,
      toCode: APPDATA.toList[APPDATA.toIndex].maerskCode,
      enabled: APPDATA.statusIndex,
      groupId: store.get("companyId")
    }
    Request.saveCompanyRoute(postData).then(res => {
      if (res.code == 200 && res.data) {
        that.setData({
          showModal: true
        })
      } else {
        mouble.wxErrorTips(res.message, false, function () {
        })
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})